<template>
  <div class="BaseInputNumber-container">
    <a-input-number v-if="constraintLength[0] < 1 && constraintLength[1] < 1" v-model="localValue" style="width: 100%" :max="max" :min="min" :placeholder="disabled ? '' : placeholder" :disabled="disabled" v-bind="$attrs" v-on="$listeners" @blur="handleOriginBlur" />
    <a-input-number v-else v-model="localValue" style="width: 100%" :max="max" :min="min" :placeholder="disabled ? '' : placeholder" :disabled="disabled" v-bind="$attrs" v-on="$listeners" @blur="handleOriginBlur" :formatter="limitNumber" :parser="limitNumber" />
  </div>
</template>

<script>
/**
 * @description: 限制输入数字长度
 * @param {number} val 值
 * @return {string}
 */
const constLength = (val,constraintLength) =>{
  let v = val.toString()
  let s = v.split('.')
  let i = s[0]
  let f = s[1]
  i = i.length > Number(constraintLength[0]) ? i.slice(0,Number(constraintLength[0])) : i
  if(v.includes('.')){
    f = f.length > Number(constraintLength[1]) ? f.slice(0,Number(constraintLength[1])) : f
  }
  return v.includes('.') ? i + '.' + f : i
}
export default {
  name: 'BaseInputNumber',
  model: {
    event: 'packageInput'
  },
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    max: {
      type: Number,
      default: 99999999999
    },
    min: {
      type: Number
    },
    //限制长度
    constraintLength:{
      type: [Array],
      default: () => [0,0]
    },
    // 是否只能输入整数
    isInt: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请输入'
    },
    disabledValue: {
      type: [String, Number],
      default: undefined
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    localValue: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('packageInput', val)
      }
    }
  },
  methods: {
    /**
     * 是否为整数
     */
    isIntValue(val) {
      return Number.isInteger(Number(val))
    },
    /**
     * 判断是否为空值，包括undefine、null、''，不包括数值0的情况
     */
    isEmptyValue(val) {
      return !val && val !== 0
    },
    handleOriginBlur(e) {
      this.$nextTick(() => {
        if (this.isInt && !Number.isSafeInteger(this.localValue) && !this.isEmptyValue(this.localValue)) {
          this.localValue = Math.floor(this.localValue)
        }
        // 设置禁用数组
        if ((this.disabledValue || this.disabledValue == 0) && this.localValue == this.disabledValue) {
          this.localValue = undefined
        }
      })
    },
    /**
     * @description: 限制输入数字长度
     * @param {number} val 值
     * @return {string}
     */
    constLength(val){
      let v = val.toString()
      let s = v.split('.')
      let i = s[0]
      let f = s[1]
      i = i.length > this.constraintLength[0] ? i.slice(0,this.constraintLength[0] ) : i
      if(v.includes('.')){
        f = f.length > this.constraintLength[1]  ? f.slice(0,this.constraintLength[1] ) : f
      }
      return v.includes('.') ? i + '.' + f : i
    },
    /**
     * @description: 限制输入数字格式
     * @param {number} val 值
     * @return {number}
     */
    limitNumber(val){
      let v = void 0
      switch(typeof val){
        case 'string':
          if(isNaN(Number(val))){
            v = void 0
          }else{
            v = this.constLength(val)
          }
          break
        case 'number':
          v = this.constLength(val)
          break
        default:
          v = void 0
      }
      return v
    }
  }
}
</script>

<style>
</style>
